<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>  
    </head>
    <body>
	
	<?php if(Yii::app()->user->hasFlash('successl')):?>
		<div align="center" style="background-color:#46BCD2;">
			<?php echo Yii::app()->user->getFlash('successl'); ?>
		</div>
	<?php endif; ?>	
	
	<?php if(Yii::app()->user->hasFlash('fails')):?>
		<div align="center" style="background-color:#46BCD2;">
			<?php echo Yii::app()->user->getFlash('fails'); ?>
		</div>
	<?php endif; ?>
	
		<table>
			<tr><td style="background:#1C86EE;color:#fff;"><b>Всего событий: <?php echo count($events)?></b></td></tr>
			<tr>
				<td style='padding:20px;border:1px solid #C9E0ED;border-radius:8px;'>
					<ol>
						<?php 
							$credentials = Yii::app()->db->createCommand()
							->select('email')
							->from('tbl_users')
							->where('username=:username', array(':username'=>Yii::app()->user->name))
							->queryRow();						
							$who = "";
							if (count($events) != 0) {
								foreach ($events as $event) 
								{
									$url1 = $event->id->text;
									$pieces = explode("/", $url1);
									$url1 = $pieces[8];
									$description = $event->content;
									
									foreach ($event->who as $w) {
										if ($w == $credentials["email"]) {continue;}
										if ($w == "elcp.conf@gmail.com") {$w = "Отдел конференций";}
										else if ($w == "elcp.promo@gmail.com") {$w = "Отдел рекламы";}
										else if ($w == "elcp.subs@gmail.com") {$w = "Отдел подписки";}
										$who = $who . $w . "</br>";
									}									
									
									foreach ($event->when as $when) {
									  $startTime = strtotime($when->startTime);
									  $endTime = strtotime($when->endTime);
									}
									
									$sdate_dd = date('d', $startTime);
									$sdate_mm = date('m', $startTime);
									$sdate_yy = date('Y', $startTime);
									$sdate_hh = date('H', $startTime);
									$sdate_ii = date('i', $startTime);
									$edate_dd = date('d', $endTime);
									$edate_mm = date('m', $endTime);
									$edate_yy = date('Y', $endTime);
									$edate_hh = date('H', $endTime);
									$edate_ii = date('i', $endTime);
									$sTime = $sdate_yy . '-' . $sdate_mm . '-' . $sdate_dd . ' ' . $sdate_hh . ':' . $sdate_ii;
									$eTime = $edate_yy . '-' . $edate_mm . '-' . $edate_dd . ' ' . $edate_hh . ':' . $edate_ii;
									
									$today = strtotime("today");
									
									if ($endTime < $today) {
										$cl = "#eee";
									} else {
										$cl = "#46BCD2";
									}
									
									echo "<li><table><tr><td style='background:".$cl."'><b><i>" . $title = ucwords($event->title->text) . "</i></b></td></tr><tr><td><br/></td></tr>";
									echo "<tr><td><b>Когда: </b>" . $sTime . " - " . $eTime . "</td></tr>";
									echo "<tr><td><b>Участники: </b>" . $who . "</td></tr>";
									echo "<tr><td><b>Описание: </b>" . $description . "</td></tr></table>";
									
									if (Yii::app()->user->name == 'sales' || Yii::app()->user->name == 'admin') {
										$updateUrl = Yii::app()->createUrl('/calendar/page', array('view'=>'edit', 'id'=>$url1));
										echo CHtml::link('Изменить', '#', array('onclick'=>"$('#u-frame').attr('src','$updateUrl'); $('#updatedialog').dialog('open'); return false;",));
										echo " | ";
										$deleteUrl = Yii::app()->createUrl('/calendar/page', array('view'=>'delete', 'id'=>$url1));
										echo CHtml::link('Удалить', '#', array('onclick'=>"$('#d-frame').attr('src','$deleteUrl'); $('#deletedialog').dialog('open'); return false;",));
									}
									echo "</li><hr/>";
								}
							}
							else {
								$url1="";
								echo "<br/><p><i>Нет активных событий...</i></p><br/>";
							}
						?>
					</ol>
				</td>
			</tr>
		</table>
		
		<?php if (Yii::app()->user->name == 'admin'): ?>
			<table align="center">
				<tr>
					<td align="center">
						<?php
							$this->widget('bootstrap.widgets.TbButton', array(
							'type'=>'primary',
							'size'=>'small', 
							'label' => 'Создать новое событие',
							'url' => '#',
							'htmlOptions' => array(
											'data-toggle' => 'modal',
											'onclick' => '$("#createdialog").dialog("open"); return false;'),
							));
						?>
					</td>
				</tr>
			</table>
		<?php endif; ?>
		
        <?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
            'id' => 'createdialog',
            'options' => array(
                'title' => 'Новое событие',
                'autoOpen' => false,
				'modal' => true,
				'resizable' => false,
                'width' => 640,
                'height' => 580
            )
        ));
        ?>
        <div align="center" style="border:1px solid #C9E0ED;border-radius:8px;padding:10px;">
            <form name="createevent" action ="" method="post">
                <table style="width:auto;" align="center">
                    <tr style="display:block">
                        <td style="width:160px;"><b>Заголовок: </b><span style="color:#CC0000;">*</span></td><td><input type="text" name="createevent" id="createevent"></td>
                    </tr>
                    <tr style="display:block">
                        <td style="width:160px;"><b>Дата начала: </b><span style="color:#CC0000;">*</span></td>
						<td>
						<?php 
							$this->widget('ext.EJuiTimePicker.EJuiTimePicker', array(
								'name'=>'startdate',
								'options' => array(
									'dateFormat' => 'yy-mm-dd',
								),
								'timeOptions' => array(
									'showOn' => 'focus',
								),
								'language' => 'ru',
								)
							);
						?>
                        </td>
                    </tr>
                    <tr style="display:block">
                        <td style="width:160px;"><b>Дата окончания: </b><span style="color:#CC0000;">*</span></td>
						<td>
						<?php 
							$this->widget('ext.EJuiTimePicker.EJuiTimePicker', array(
								'name'=>'enddate',
								'options' => array(
									'dateFormat' => 'yy-mm-dd',
								),
								'timeOptions' => array(
									'showOn' => 'focus',
								),
								'language' => 'ru',
								)
							);
						?>
                        </td>
                    </tr>
                    <tr style="display:block">
                        <td style="width:160px;"><b>Место проведения: </b></td><td><input type="text" name="meeting_location" id="meeting_location"></td>
                    </tr>					
                    <tr style="display:block">
                        <td style="width:160px;"><b>Описание:</b></td><td><textarea name="desc" id="desc" rows="6"></textarea></td>
                    </tr>
					<tr style="display:block">
						<td style="width:160px;"><b>Пригласить: </b><span style="color:#CC0000;">**</span></td>
						<td>
							<select multiple="multiple" id="invite" name="guests[]" onchange='$.fn.myFunction(); return false;'>							
								<option value="elcp.conf@gmail.com">Отдел конференций</option>
								<option value="elcp.subs@gmail.com">Отдел подписки</option>
								<option value="elcp.promo@gmail.com">Отдел рекламы</option>
								<option value="input">Ввести вручную</option>							  
							</select>
						</td>
					</tr>
                    <tr style="display:none" id="fb_from_name">
                        <td style="width:160px;"><b>Ваше имя: </b><span style="color:#CC0000;">*</span></td><td><input type="text" name="from_name"></td>
                    </tr>	
                    <tr style="display:none" id="fb_mtext">
                        <td style="width:160px;"><b>Текст письма:</b></td><td><textarea name="mtext" rows="6"></textarea></td>
                    </tr>	
					<tr style="display:none" id="fb_email">
						<td style="width:160px;"><b>Email адреса: </b><span style="color:#CC0000;">*</span></td><td><textarea name="guests2" rows="6"></textarea></td>
					</tr>					
					<script type="text/javascript">
						$.fn.myFunction = function() {
							if ($('#invite').val() == "input") {
								document.getElementById('fb_from_name').style.display='block';
								document.getElementById('fb_mtext').style.display='block';
								document.getElementById('fb_email').style.display='block';								
							}
							/*else {
								document.getElementById('fb_from_name').style.display='none';
								document.getElementById('fb_mtext').style.display='none';
								document.getElementById('fb_email').style.display='none';							
							}*/
						}
					</script>					
					<tr style="display:block"><td style="width:160px;"></td><td style="font-size: 8pt;"><span style="color:#CC0000;">* Поля обязательные для заполнения</span></td></tr>
					<tr style="display:block"><td style="width:160px;"></td><td style="font-size: 8pt;"><span style="color:#CC0000;">** Для выбора нескольких значений - удерживать Ctrl</span></td></tr>
					<tr>
						<td>
							<input type="submit" name="submit" value="Создать">
						</td>
						<td>
							<?php
								$this->widget('bootstrap.widgets.TbButton', array(
									'label' => 'Отмена',
									'size' => 'small', 
									'htmlOptions' => array(
												'onclick' => "window.parent.$('#createdialog').dialog('close');"),
								));
							?>
						</td>
					</tr>					
                </table>
            </form>
        </div>
        <?php $this->endWidget('zii.widgets.jui.CJuiDialog'); ?>
	
        <?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
				'id' => 'updatedialog',
				'options' => array(
					'title' => 'Редактировать событие',
					'autoOpen' => false,
					'width' => 640,
					'height' => 600,
					'modal' => true,
					'resizable' => false,
					'close'=>'js:function(){window.location.reload();}',
				),
			));
		?>

		<div align="center" style="width:100%;height:100%">
			<iframe id="u-frame" width="100%" height="100%"></iframe>
		</div>

		<?php $this->endWidget('zii.widgets.jui.CJuiDialog'); ?>

        <?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
            'id' => 'deletedialog',
            'options' => array(
                'title' => 'Удалить событие',
                'autoOpen' => false,
				'resizable' => false,
                'width' => 500,
                'height' => 200,
				'modal'=>true,
				'close'=>'js:function(){window.location.reload();}',
            )
        ));
        ?>
		
		<div align="center">
			<iframe id="d-frame" width="100%" height="100%"></iframe>
		</div>
		
		<?php $this->endWidget('zii.widgets.jui.CJuiDialog'); ?>
	
    </body>
</html>


